﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Golf.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace Golf.DAL
{
    /// <summary>
    /// 数据访问类:Golf_OrderProduct
    /// </summary>
    public partial class Golf_OrderProduct : IGolf_OrderProduct
    {
        public Golf_OrderProduct()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("Id", "Golf_OrderProduct");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int result = DbHelperSQL.RunProcedure("Golf_OrderProduct_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(Golf.Model.Golf_OrderProduct model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@OrderId", SqlDbType.Int,4),
					new SqlParameter("@OrderNumber", SqlDbType.NVarChar,15),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@FristName", SqlDbType.NVarChar,100),
					new SqlParameter("@LastName", SqlDbType.NVarChar,100),
					new SqlParameter("@ProductTypeId", SqlDbType.Int,4),
					new SqlParameter("@ProductId", SqlDbType.Int,4),
					new SqlParameter("@ProductName", SqlDbType.NVarChar,200),
					new SqlParameter("@GiftId", SqlDbType.NVarChar,100),
					new SqlParameter("@GiftNumber", SqlDbType.Int,4),
					new SqlParameter("@ProductPrice", SqlDbType.Decimal,9),
					new SqlParameter("@BuyNumber", SqlDbType.Int,4),
					new SqlParameter("@Integral", SqlDbType.Int,4),
					new SqlParameter("@Pro_Specification", SqlDbType.NVarChar,100),
					new SqlParameter("@AdminId", SqlDbType.Int,4),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@IsStae", SqlDbType.Int,4),
					new SqlParameter("@Product_Attr", SqlDbType.NVarChar,200)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.OrderId;
            parameters[2].Value = model.OrderNumber;
            parameters[3].Value = model.UserId;
            parameters[4].Value = model.FristName;
            parameters[5].Value = model.LastName;
            parameters[6].Value = model.ProductTypeId;
            parameters[7].Value = model.ProductId;
            parameters[8].Value = model.ProductName;
            parameters[9].Value = model.GiftId;
            parameters[10].Value = model.GiftNumber;
            parameters[11].Value = model.ProductPrice;
            parameters[12].Value = model.BuyNumber;
            parameters[13].Value = model.Integral;
            parameters[14].Value = model.Pro_Specification;
            parameters[15].Value = model.AdminId;
            parameters[16].Value = model.AddTime;
            parameters[17].Value = model.CreateTime;
            parameters[18].Value = model.IsStae;
            parameters[19].Value = model.Product_Attr;

            DbHelperSQL.RunProcedure("Golf_OrderProduct_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public bool Update(Golf.Model.Golf_OrderProduct model)
        {
            int rowsAffected = 0;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@OrderId", SqlDbType.Int,4),
					new SqlParameter("@OrderNumber", SqlDbType.NVarChar,15),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@FristName", SqlDbType.NVarChar,100),
					new SqlParameter("@LastName", SqlDbType.NVarChar,100),
					new SqlParameter("@ProductTypeId", SqlDbType.Int,4),
					new SqlParameter("@ProductId", SqlDbType.Int,4),
					new SqlParameter("@ProductName", SqlDbType.NVarChar,200),
					new SqlParameter("@GiftId", SqlDbType.NVarChar,100),
					new SqlParameter("@GiftNumber", SqlDbType.Int,4),
					new SqlParameter("@ProductPrice", SqlDbType.Decimal,9),
					new SqlParameter("@BuyNumber", SqlDbType.Int,4),
					new SqlParameter("@Integral", SqlDbType.Int,4),
					new SqlParameter("@Pro_Specification", SqlDbType.NVarChar,100),
					new SqlParameter("@AdminId", SqlDbType.Int,4),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@IsStae", SqlDbType.Int,4),
					new SqlParameter("@Product_Attr", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.OrderId;
            parameters[2].Value = model.OrderNumber;
            parameters[3].Value = model.UserId;
            parameters[4].Value = model.FristName;
            parameters[5].Value = model.LastName;
            parameters[6].Value = model.ProductTypeId;
            parameters[7].Value = model.ProductId;
            parameters[8].Value = model.ProductName;
            parameters[9].Value = model.GiftId;
            parameters[10].Value = model.GiftNumber;
            parameters[11].Value = model.ProductPrice;
            parameters[12].Value = model.BuyNumber;
            parameters[13].Value = model.Integral;
            parameters[14].Value = model.Pro_Specification;
            parameters[15].Value = model.AdminId;
            parameters[16].Value = model.AddTime;
            parameters[17].Value = model.CreateTime;
            parameters[18].Value = model.IsStae;
            parameters[19].Value = model.Product_Attr;

            DbHelperSQL.RunProcedure("Golf_OrderProduct_Update", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            int rowsAffected = 0;
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            DbHelperSQL.RunProcedure("Golf_OrderProduct_Delete", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Golf_OrderProduct ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Golf.Model.Golf_OrderProduct GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            Golf.Model.Golf_OrderProduct model = new Golf.Model.Golf_OrderProduct();
            DataSet ds = DbHelperSQL.RunProcedure("Golf_OrderProduct_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderId"] != null && ds.Tables[0].Rows[0]["OrderId"].ToString() != "")
                {
                    model.OrderId = int.Parse(ds.Tables[0].Rows[0]["OrderId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderNumber"] != null && ds.Tables[0].Rows[0]["OrderNumber"].ToString() != "")
                {
                    model.OrderNumber = ds.Tables[0].Rows[0]["OrderNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UserId"] != null && ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FristName"] != null && ds.Tables[0].Rows[0]["FristName"].ToString() != "")
                {
                    model.FristName = ds.Tables[0].Rows[0]["FristName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LastName"] != null && ds.Tables[0].Rows[0]["LastName"].ToString() != "")
                {
                    model.LastName = ds.Tables[0].Rows[0]["LastName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ProductTypeId"] != null && ds.Tables[0].Rows[0]["ProductTypeId"].ToString() != "")
                {
                    model.ProductTypeId = int.Parse(ds.Tables[0].Rows[0]["ProductTypeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProductId"] != null && ds.Tables[0].Rows[0]["ProductId"].ToString() != "")
                {
                    model.ProductId = int.Parse(ds.Tables[0].Rows[0]["ProductId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProductName"] != null && ds.Tables[0].Rows[0]["ProductName"].ToString() != "")
                {
                    model.ProductName = ds.Tables[0].Rows[0]["ProductName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["GiftId"] != null && ds.Tables[0].Rows[0]["GiftId"].ToString() != "")
                {
                    model.GiftId = ds.Tables[0].Rows[0]["GiftId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["GiftNumber"] != null && ds.Tables[0].Rows[0]["GiftNumber"].ToString() != "")
                {
                    model.GiftNumber = int.Parse(ds.Tables[0].Rows[0]["GiftNumber"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProductPrice"] != null && ds.Tables[0].Rows[0]["ProductPrice"].ToString() != "")
                {
                    model.ProductPrice = decimal.Parse(ds.Tables[0].Rows[0]["ProductPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BuyNumber"] != null && ds.Tables[0].Rows[0]["BuyNumber"].ToString() != "")
                {
                    model.BuyNumber = int.Parse(ds.Tables[0].Rows[0]["BuyNumber"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Integral"] != null && ds.Tables[0].Rows[0]["Integral"].ToString() != "")
                {
                    model.Integral = int.Parse(ds.Tables[0].Rows[0]["Integral"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Pro_Specification"] != null && ds.Tables[0].Rows[0]["Pro_Specification"].ToString() != "")
                {
                    model.Pro_Specification = ds.Tables[0].Rows[0]["Pro_Specification"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AdminId"] != null && ds.Tables[0].Rows[0]["AdminId"].ToString() != "")
                {
                    model.AdminId = int.Parse(ds.Tables[0].Rows[0]["AdminId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddTime"] != null && ds.Tables[0].Rows[0]["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateTime"] != null && ds.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsStae"] != null && ds.Tables[0].Rows[0]["IsStae"].ToString() != "")
                {
                    model.IsStae = int.Parse(ds.Tables[0].Rows[0]["IsStae"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Product_Attr"] != null && ds.Tables[0].Rows[0]["Product_Attr"].ToString() != "")
                {
                    model.Product_Attr = ds.Tables[0].Rows[0]["Product_Attr"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrderId,OrderNumber,UserId,FristName,LastName,ProductTypeId,ProductId,ProductName,GiftId,GiftNumber,ProductPrice,BuyNumber,Integral,Pro_Specification,AdminId,AddTime,CreateTime,IsStae,Product_Attr ");
            strSql.Append(" FROM Golf_OrderProduct ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,OrderId,OrderNumber,UserId,FristName,LastName,ProductTypeId,ProductId,ProductName,GiftId,GiftNumber,ProductPrice,BuyNumber,Integral,Pro_Specification,AdminId,AddTime,CreateTime,IsStae,Product_Attr ");
            strSql.Append(" FROM Golf_OrderProduct ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Golf_OrderProduct ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from Golf_OrderProduct T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "Golf_OrderProduct";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 1;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }

        #endregion  Method
    }
}

